package control;

import java.io.IOException;
import java.util.Collection;
import java.util.TreeSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modele.Med;
import modele.Dep;
import modele.Pays;
import modele.Spe;

public class Controleur extends HttpServlet {

    private Pays p;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        p = new Pays();

    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


        String page = null;
        String action = request.getParameter("action");
        String choix = request.getParameter("choix");

        if (action == null) {
            page = "menu.jsp";

        } else if (action.equals("parDep")) {
            if (choix == null) {
                Collection<Dep> cold = p.getLesDeps();
                request.setAttribute("lesDeps", cold);
                page = "listeDeps.jsp";
            } else {
                Collection<Med> colm = p.getLeDep(choix).lesMeds();
                request.setAttribute("lesMeds", colm);
                page = "listeMed.jsp";
            }

        } else if (action.equals("parNom")) {
            String nom = request.getParameter("nom");
            if (nom == null) {
                page = "choixNom.jsp";
            } else {

                Collection<Med> lesMeds = new TreeSet<Med>();
                for (Dep leDep : p.getLesDeps()) {
                    for (Med leMed : leDep.lesMeds()) {
                        if (leMed.getNom().startsWith(nom.substring(0, 1).toUpperCase())) {
                            lesMeds.add(leMed);
                        }

                    }
                }
                request.setAttribute("lesMeds", lesMeds);
                page = "listeMed.jsp";
            }

        } else if (action.equals("parSpe")) {





            if (choix == null) {
                Collection<Spe> cold = p.getLesSpec();
                request.setAttribute("lesSpe", cold);
                page = "spe.jsp";

            } else {
                Collection<Med> colm = p.getLaSpe(choix).getLesMeds();
                request.setAttribute("lesMeds", colm);
                page = "listeMed.jsp";
            }
        }



        request.getRequestDispatcher(page).forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method. /
     *
     **
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}